﻿using System.Linq;
using Microsoft.WowAddonStudio.TocEditor;
using NUnit.Framework;

namespace WowProjectPackage.Tests.TocEditor
{
	[TestFixture]
	public class MetadataManagerTest
	{
		private IMetadataManager manager;
		private TocMetadata metadata;

		[SetUp]
		public void SetUp()
		{
			var doc = new TocDocument();
			metadata = doc.Tags;
			manager = new MetadataManager(doc);
		}

		[TearDown]
		public void TearDown()
		{
		}

		[Test]
		public void TestAddBasicMetadataWithNullValue()
		{
			manager[TocTagName.Interface] = null;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			//Basic metadata cannot be added with null value...
			Assert.AreEqual(metadata.GetTags().Count(), 0);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 0);
		}

		[Test]
		public void TestAddBasicMetadataWithEmptyValue()
		{
			manager[TocTagName.Interface] = string.Empty;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			//Basic metadata cannot be added with empty value...
			Assert.AreEqual(metadata.GetTags().Count(), 0);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 0);
		}

		[Test]
		public void TestAddBasicMetadataWithPlainValue()
		{
			manager[TocTagName.Interface] = "1223";
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 1);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 0);

			Assert.AreEqual(manager.GetBasicTags().First().Key, TocTagName.Interface);
			Assert.AreEqual(manager.GetBasicTags().First().Value, "1223");
		}

		[Test]
		public void TestAddExtendedMetadataWithPlainValue1()
		{
			manager[TocTagName.XEmail] = "someone@somewhere.com";
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, TocTagName.XEmail);
			Assert.AreEqual(manager.GetExtendedTags().First().Value, "someone@somewhere.com");
		}

		[Test]
		public void TestAddExtendedMetadataWithEmptyValue()
		{
			manager[TocTagName.XEmail] = string.Empty;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, TocTagName.XEmail);
			Assert.AreEqual(manager.GetExtendedTags().First().Value, string.Empty);
		}

		[Test]
		public void TestAddExtendedMetadataWithNullValue()
		{
			manager[TocTagName.XEmail] = null;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, TocTagName.XEmail);
			Assert.AreEqual(manager.GetExtendedTags().First().Value, null);
		}

		[Test]
		public void TestAddCustomMetadataWithPlainValue()
		{
			manager["custom"] = "customvalue";
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, "custom");
			Assert.AreEqual(manager.GetExtendedTags().First().Value, "customvalue");
		}

		[Test]
		public void TestAddCustomMetadataWithEmptyValue()
		{
			manager["custom"] = string.Empty;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, "custom");
			Assert.AreEqual(manager.GetExtendedTags().First().Value, string.Empty);
		}

		[Test]
		public void TestAddCustomMetadataWithNullValue()
		{
			manager["custom"] = null;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			Assert.AreEqual(manager.GetExtendedTags().First().Key, "custom");
			Assert.AreEqual(manager.GetExtendedTags().First().Value, null);
		}

		[Test]
		public void TestRemoveTag()
		{
			manager["custom"] = null;
			Assert.IsNotNull(metadata.GetTags());
			Assert.IsNotNull(manager.GetBasicTags());
			Assert.IsNotNull(manager.GetExtendedTags());

			Assert.AreEqual(metadata.GetTags().Count(), 1);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 1);

			manager.Remove("custom");

			Assert.AreEqual(metadata.GetTags().Count(), 0);
			Assert.AreEqual(manager.GetBasicTags().Count(), 0);
			Assert.AreEqual(manager.GetExtendedTags().Count(), 0);
		}
	}
}